import axios from '../../../utils/http'
import XLSX from 'xlsx'
import Cookies from 'js-cookie'
import {baseURL} from '../../../utils/http'
// 查询学期
export async function getTermList() {
  const {
    data
  } = await axios.get('/bac/term/getTermList')
  return data
}


// 根据学期查询周次
export async function getWeekList(parameter) {
  const {
    data
  } = await axios.get('/bac/weekDate/getWeekListByTerm?termId=' + parameter)
  return data
}

// 查询学生抽查计划 /dormNightSpotPlan/selectSpotPlans
export async function getSpotList(parameter) {
  const {
    data
  } = await axios.get('/dormNight/spotPlan' + parameter)
  return data
}

export async function getDormitory(buildingName) {
  const {
    data
  } = await axios.post('/bac/building/getDormitoryList',{
    buildingName:buildingName
  })
  return data
}


// 添加 学生计划
// export async function addSpotDetails(stuOneName, stuTwoName, remark,buidingName,week, termName) {
  // const {
    // data
  // } = await axios.post('/dormNight/spotPlan', {
    //  buidingName: buidingName,
    //  remark: remark,
    //  stuOneName: stuOneName,
    //  stuTwoName: stuTwoName,
    //  termName: termName,
    //  week: week
  // })
  // return data
// }
// 添加 值班情况/dormNight/spotPlan单条插入抽查计划
export async function addSpotDetails(data) {
  return axios.post('/dormNight/spotPlan', data)
}
// /dormNight/spotLog 学生值班计划分配   新增学生值班计划
export async function addDistributionData( spotDate, roomName, planId) {
  const {
    data
  } = await axios.post('/dormNight/spotLog', {
    spotDate: spotDate,
    roomName: roomName,
    planId: planId

  })
  return data
}

// /dormNight/spotLog   批量删除值班计划 deleteDistributionData
export async function deleteDistributionData(date) {
  const {
    data
  } = await axios.delete('/dormNight/spotLog', {
    data: date,
  })
  return data
}
//  学生值班计划分配   修改学生值班计划/dormNight/spotLog
export async function upDataDistributionData( spotDate, roomName, planId   ) {
  const {
    data
  } = await axios.put('/dormNight/spotLog', {
    spotDate: spotDate,
    roomName: roomName,
    planId: planId
  })
  return data
}

// 修改 学生值班计划  /iseSchedule/schedule
export async function updataSpotDetails(stuOneName, stuTwoName, id, remark) {
  const {
    data
  } = await axios.put('/dormNight/spotPlan', {
    stuOneName: stuOneName,
    stuTwoName: stuTwoName,
    id: id,
    remark: remark
  })
  return data
}

// 删除学生值班计划
export async function deleteSpotDetails(scheduleIds) {
  const {
    data
  } = await axios.delete('/dormNight/spotPlan', {
    data: scheduleIds,
  })
  return data
}
// 学生值班计划分配
   export async function getDistributionList(parameter) {
     const {
       data
     } = await axios.get('/dormNight/spotLog' + parameter)
     return data
   }
// /dormNight/spotLog/getDeatilList     查看学生值班计划分配详情

export async function getDistribution(parameter) {
  const {
    data
  } = await axios.get('/dormNight/spotLog/getDeatilList' + parameter)
  return data
}



// Excel导入数据处理
export function excel(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()
    reader.readAsArrayBuffer(file)
    reader.onerror = e => {
      reject(e)
    }
    reader.onload = e => {
      const excelData = e.target.result
      const wb = XLSX.read(excelData, {
        type: 'array'
      })
      const firstSheetName = wb.SheetNames[0]
      const worksheet = wb.Sheets[firstSheetName]
      const columns = processHeader(worksheet)
      const tableData = XLSX.utils.sheet_to_json(columns)
      if (!tableData[0].学期 && !tableData[0].周次) {
        resolve(false)
      }
      resolve(tableData)
    }
  })


}

// 格式化Excel数据 表头
function processHeader(sheet) {
  const range = XLSX.utils.decode_range(sheet['!ref'])
  const C = range.e.c
  for (let i = 0; i <= C; i++) {
    sheet[String.fromCharCode(65 + i) + 1] = sheet[String.fromCharCode(65 + i) + 2]
    delete sheet[String.fromCharCode(65 + i) + 2]
  }
  return sheet
}

// 添加导入Excel数据
export async function addExceldata(excelData) {
  const {
    data
  } = await axios.post('/dormNight/spotPlan/uploadExcel', excelData)
  return data
}

// 导出Excel数据
export function getExcel() {
  fetch(baseURL+'/dormNight/spotPlan/download', {
      method: 'GET',
      headers: {
        'JHC-Authorization': Cookies.get('token')
      },
    })
    .then(res => {
      return res.blob()
    })
    .then(data => {
      const blobUrl = window.URL.createObjectURL(data)
      download(blobUrl)
    })
}
//getselfExcel自习表的导出
export function getselfExcel() {
  fetch(baseURL+'/dormNight/spotPlan/download', {
      method: 'GET',
      headers: {
        'JHC-Authorization': Cookies.get('token')
      },
    })
    .then(res => {
      return res.blob()
    })
    .then(data => {
      const blobUrl = window.URL.createObjectURL(data)
      download(blobUrl)
    })
}
// downloadShuttering
export function downloadShuttering() {
  fetch(baseURL+'study/downloadTemplate', {
      method: 'GET',
      headers: {
        'JHC-Authorization': Cookies.get('token')
      },
    })
    .then(res => {
      return res.blob()
    })
    .then(data => {
      const blobUrl = window.URL.createObjectURL(data)
      downloads(blobUrl)
    })
}
export function downloadWorkShuttering() {
  fetch(baseURL+'/dormNight/spotPlan/downLoadTemplate', {
      method: 'GET',
      headers: {
        'JHC-Authorization': Cookies.get('token')
      },
    })
    .then(res => {
      return res.blob()
    })
    .then(data => {
      const blobUrl = window.URL.createObjectURL(data)
      downloadWork(blobUrl)
    })
}

function download(blobUrl) {
  const a = document.createElement('a')
  a.download = '学生抽查计划表'

  a.href = blobUrl
  a.click()
}

function downloads(blobUrl) {
  const a = document.createElement('a')
  a.download = '自习安排表模板' + '.xlsx'
  a.href = blobUrl
  a.click()
}

function downloadWork(blobUrl) {
  const a = document.createElement('a')
  a.download = '学生抽查计划模板' + '.xlsx'
  a.href = blobUrl
  a.click()
}

// 查询学生列表的模糊查询  /teacher/teacherListByLikeName
export async function getStudentListByLikeName(parameter) {
  const {
    data
  } = await axios.get('/cms/student/userListByLikeName',{
    params:{
      name: parameter
    }
  })
  return data
}

export async function getWeekTime(termName, week) {
  return axios.get('/bac/weekDate/getTimeByTermNameWeek', {
    params: {
      termName: termName,
      week: week
    }
  })
}
